﻿using EmployeeLeaveSystem.Shared.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace EmployeeLeaveSystem.Server {
    // DataBase
    public class EpMainContext : DbContext {
        public EpMainContext(DbContextOptions<EpMainContext> options) : base(options) { }

        // 配置输出的数据库显示实际的数据源
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder
        .EnableSensitiveDataLogging();
        //.LogTo(Console.WriteLine)


        protected override void OnModelCreating(ModelBuilder modelBuilder) {

            modelBuilder.Entity<LeaveDetail>().
                HasOne(A => A.Staff).
                WithMany(T => T.LeaveDetail).
                HasForeignKey(T => T.StaffId).
                OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<LeaveDetail>().
                HasOne(A => A.AcceptStaff).
                WithMany(T => T.AcceptHistory).
                HasForeignKey(T => T.AcceptStaffId).
                OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<StaffInfo>().
               HasMany(A => A.childStaffs).
               WithOne(B => B.UpperStaff).
               HasForeignKey(A => A.UpperStaffId).
               OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<StaffInfo>().
               HasMany(A => A.LeaveDetail).
               WithOne(B => B.Staff).
               HasForeignKey(B => B.StaffId).
               OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<StaffInfo>().
               HasMany(A => A.AcceptHistory).
               WithOne(B => B.AcceptStaff).
               HasForeignKey(B => B.AcceptStaffId).
               OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<StaffInfo>().
               HasMany(A => A.AddWOMs).
               WithOne(B => B.Staff).
               HasForeignKey(B => B.StaffId).
               OnDelete(DeleteBehavior.Restrict);

        }
        public DbSet<StaffInfo> StaffInfos { get; set; }
        // 请假信息表格
        public DbSet<LeaveDetail> LeaveDetailInfos { get; set; }
        public DbSet<Department> Departments { get; set; }
        public DbSet<Comments> Comments { get; set; }
        public DbSet<FileInfos> FileInfos { get; set; }
        // 人事调动系统功能实现
        public DbSet<AddMoveDepartment> AddMoves { get; set; }
        public DbSet<AddRetireMove> AddRetireMoves { get; set; }
        // 加班管理表实现
        public DbSet<AddWOM> AddWOMs { get; set; }
        // 考情管理系统实现
        public DbSet<AddWork> AddWorks { get; set; }
        public DbSet<NowRules> NowRules { get; set; }// 当前系统设定的规则
        // 薪水管理记录
        public DbSet<SalaryInfo> SalaryInfos { get; set; }
    }
}
//数据库链接常用指令
/**
 * Add-Migration Init
 * Update-DataBase
 * drop-database -Context "EpMainContext"
 * 
 */


/* 创建AddWOMs表格的数据库代码
       CREATE TABLE [AddWOMs] (
          [Id] int NOT NULL IDENTITY,
          [StaffId] int NOT NULL,
          [Created] datetime2 NOT NULL,
          [StartTime] datetime2 NOT NULL,
          [EndTime] datetime2 NOT NULL,
          [Reason] nvarchar(max) NOT NULL,
          [AcceptStaffId] int NULL,
          [Type] int NOT NULL,
          [AcceptTag] int NOT NULL,
          CONSTRAINT [PK_AddWOMs] PRIMARY KEY ([Id]),
          CONSTRAINT [FK_AddWOMs_StaffInfos_AcceptStaffId] FOREIGN KEY ([AcceptStaffId]) REFERENCES [StaffInfos] ([Id]),
          CONSTRAINT [FK_AddWOMs_StaffInfos_StaffId] FOREIGN KEY ([StaffId]) REFERENCES [StaffInfos] ([Id]) ON DELETE CASCADE
      );
 */

/* 建立Index
  CREATE INDEX [IX_NowRules_StaffId] ON [NowRules] ([StaffId]);
 */